﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QL_BenXe
{
    public partial class frm_QLLichXeChay : Form
    {
        public frm_QLLichXeChay()
        {
            InitializeComponent();
        }

        private void frm_QLLichXeChay_Load(object sender, EventArgs e)
        {
            dtg_LichXeChay.AutoGenerateColumns = false;
            dtg_LichXeChay.DataSource = LichChayBUS.Select_All();
            dateTimePicker1.MinDate = DateTime.Today;
            LoadData();
        }

        private void LoadData()
        {
            cmb_TuyenDuong.DisplayMember = "TenTuyenDuong";
            cmb_TuyenDuong.ValueMember = "MaTuyenDuong";
            cmb_TuyenDuong.DataSource = TuyenDuongBUS.Select_All();

            cmb_Xe.DisplayMember = "BienSo";
            cmb_Xe.ValueMember = "MaXe";
            cmb_Xe.DataSource = XeBUS.Select_All();

            cmb_TaiChinh.DisplayMember = "TenNhanVien";
            cmb_TaiChinh.ValueMember = "MaNhanVien";
            cmb_TaiChinh.DataSource = NhanVienBUS.Select_TaiXe();

            cmb_TaiPhu.DisplayMember = "TenNhanVien";
            cmb_TaiPhu.ValueMember = "MaNhanVien";
            cmb_TaiPhu.DataSource = NhanVienBUS.Select_TaiXe();

            cmb_TiepVien.DisplayMember = "TenNhanVien";
            cmb_TiepVien.ValueMember = "MaNhanVien";
            cmb_TiepVien.DataSource = NhanVienBUS.Select_TiepVien();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            try
            {
                LichChay emp = new LichChay();
                emp.MaXe = cmb_Xe.SelectedValue.ToString();
                emp.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
                emp.Ngay = dateTimePicker1.Value.ToShortDateString();
                emp.GioXuatBen = txt_Gio.Text + ":" + txt_Phut.Text;
                emp.TaiChinh = cmb_TaiChinh.SelectedValue.ToString();
                emp.TaiPhu = cmb_TaiPhu.SelectedValue.ToString();
                emp.TiepVien = cmb_TiepVien.SelectedValue.ToString();

                bool b = LichChayBUS.Insert(emp);
                if (b)
                {
                    MessageBox.Show("Thêm thành công !!!");
                    dtg_LichXeChay.DataSource = LichChayBUS.Select_All();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = LichChayBUS.Delete(MaXe,MaTD,Ngay,GioXB);
                if (b)
                {
                    MessageBox.Show("Xóa thành công !!!");
                    dtg_LichXeChay.DataSource = LichChayBUS.Select_All();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        String MaXe; String MaTD; String Ngay; String GioXB;
        private void dtg_LichXeChay_SelectionChanged(object sender, EventArgs e)
        {
            MaXe = dtg_LichXeChay.CurrentRow.Cells[7].Value.ToString();
            MaTD = dtg_LichXeChay.CurrentRow.Cells[8].Value.ToString();
            Ngay = dtg_LichXeChay.CurrentRow.Cells[2].Value.ToString();
            GioXB = dtg_LichXeChay.CurrentRow.Cells[3].Value.ToString();

            cmb_Xe.Text = dtg_LichXeChay.CurrentRow.Cells[0].Value.ToString();
            cmb_TuyenDuong.Text = dtg_LichXeChay.CurrentRow.Cells[1].Value.ToString();
            dateTimePicker1.Value = DateTime.Parse(dtg_LichXeChay.CurrentRow.Cells[2].Value.ToString());
            txt_Gio.Text = GioXB.Split(':')[0];
            txt_Phut.Text = GioXB.Split(':')[1];
            cmb_TaiChinh.Text = dtg_LichXeChay.CurrentRow.Cells[4].Value.ToString();
            cmb_TaiPhu.Text = dtg_LichXeChay.CurrentRow.Cells[5].Value.ToString();
            cmb_TiepVien.Text = dtg_LichXeChay.CurrentRow.Cells[6].Value.ToString();
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                LichChay emp_moi = new LichChay();
                emp_moi.MaXe = cmb_Xe.SelectedValue.ToString();
                emp_moi.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
                emp_moi.Ngay = dateTimePicker1.Value.ToShortDateString();
                emp_moi.GioXuatBen = txt_Gio.Text + ":" + txt_Phut.Text;
                emp_moi.TaiChinh = cmb_TaiChinh.SelectedValue.ToString();
                emp_moi.TaiPhu = cmb_TaiPhu.SelectedValue.ToString();
                emp_moi.TiepVien = cmb_TiepVien.SelectedValue.ToString();

                LichChay emp_cu = new LichChay();
                emp_cu.MaXe = MaXe;
                emp_cu.MaTuyenDuong = MaTD;
                emp_cu.Ngay = Ngay;
                emp_cu.GioXuatBen = GioXB;

                bool b = LichChayBUS.Update(emp_cu,emp_moi);
                if (b)
                {
                    MessageBox.Show("Cập nhật thành công !!!");
                    dtg_LichXeChay.DataSource = LichChayBUS.Select_All();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txt_TimKiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                dtg_LichXeChay.DataSource = LichChayBUS.Select_by_TenTuyenDuong(txt_TimKiem.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
